{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.先导入需要的模块\n",
    "import requests\n",
    "\n",
    "# 2.输入我们api_secret、api_key\n",
    "api_secret = \"dJZK70wqWOpvXYKpdBwXS5PVdZ4u0HMZ\"\n",
    "api_key = 'cYreV8BIHl_BNjOkwJI9_tLujB3TdMF2'\n",
    "\n",
    "# 3.目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'http://huangjieqi.gitee.io/picture_storage/EdisonQXF.jpg'\n",
    "\n",
    "# 4.沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5.准备后面的数据\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    # 是否检测并返回根据人脸特征判断出的年龄、性别、情绪等属性。\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "\n",
    "# 6.requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1603519957,0e57c4a7-94d9-49d4-b3bf-1806c74a07f6\",\"time_used\":335,\"faces\":[{\"face_token\":\"e20d625f29c97c64833ed2e31ea9f7ea\",\"face_rectangle\":{\"top\":161,\"left\":109,\"width\":188,\"height\":188},\"attributes\":{\"gender\":{\"value\":\"Male\"},\"age\":{\"value\":21},\"smile\":{\"value\":0.027,\"threshold\":50.000},\"emotion\":{\"anger\":0.307,\"disgust\":0.142,\"fear\":0.038,\"happiness\":0.280,\"neutral\":98.710,\"sadness\":0.150,\"surprise\":0.373}}}],\"image_id\":\"WDzfrL9g9smt0d3OoYw31A==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603519957,0e57c4a7-94d9-49d4-b3bf-1806c74a07f6',\n",
       " 'time_used': 335,\n",
       " 'faces': [{'face_token': 'e20d625f29c97c64833ed2e31ea9f7ea',\n",
       "   'face_rectangle': {'top': 161, 'left': 109, 'width': 188, 'height': 188},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 21},\n",
       "    'smile': {'value': 0.027, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.307,\n",
       "     'disgust': 0.142,\n",
       "     'fear': 0.038,\n",
       "     'happiness': 0.28,\n",
       "     'neutral': 98.71,\n",
       "     'sadness': 0.15,\n",
       "     'surprise': 0.373}}}],\n",
       " 'image_id': 'WDzfrL9g9smt0d3OoYw31A==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json() \n",
    "results"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
